package org.cst8277.ums.controller;

import lombok.extern.slf4j.Slf4j;
import org.cst8277.ums.dto.UserDto;
import org.cst8277.ums.response.ResponseMessage;
import org.cst8277.ums.service.IUserService;
import org.cst8277.ums.util.Constants;
import org.cst8277.ums.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private IUserService userService;

    @RequestMapping(method = RequestMethod.POST, path = "/login", consumes = Constants.APPLICATION_JSON)
    public Mono<ResponseEntity<Map<String, Object>>> login(@RequestBody UserDto userDto) {
        Map<String, Object> response = new HashMap<>();
        log.info("开始登录, userDto:{}", userDto);

        ResponseMessage responseMessage = userService.login(userDto);

        if (responseMessage.isSuccess() == false) {
            response.put(Constants.CODE, "500");
            response.put(Constants.MESSAGE, "login failed, " + responseMessage.getMessage());
            response.put(Constants.DATA, "Check username or password.");
        } else {
            response.put(Constants.CODE, "201");
            response.put(Constants.MESSAGE, "login successfully.");
            String token = Md5Util.md5sum("" + System.currentTimeMillis());
            response.put(Constants.DATA, token);
        }
        //TODO 增加对lastlogin的操作
        return Mono.just(ResponseEntity.ok().header(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON)
                .header(Constants.ACCEPT, Constants.APPLICATION_JSON).body(response));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/logout", consumes = Constants.APPLICATION_JSON)
    public Mono<ResponseEntity<Map<String, Object>>> logout(@RequestBody UserDto userDto) {
        Map<String, Object> response = new HashMap<>();
        log.info("开始登录, userDto:{}", userDto);

        ResponseMessage responseMessage = userService.logout(userDto);
        if (responseMessage.isSuccess() == false) {
            response.put(Constants.CODE, "500");
            response.put(Constants.MESSAGE, "login failed, " + responseMessage.getMessage());
            response.put(Constants.DATA, "Check username or password.");
        } else {
            response.put(Constants.CODE, "201");
            response.put(Constants.MESSAGE, "logout successfully.");
            response.put(Constants.DATA, "logout successfully.");
        }
        return Mono.just(ResponseEntity.ok().header(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON)
                .header(Constants.ACCEPT, Constants.APPLICATION_JSON).body(response));
    }


}
